package com.qf.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import com.qf.service.CouponService;
import com.qf.service.CouponUserService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/coupon")
//@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")//取日期时使用
//@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")//存日期时使用
public class CouponController {
    @Autowired
    private CouponService couponService;

    @Autowired
    private CouponUserService couponUserService;


    // 优惠劵列表
    //http://localhost:8083/admin-ui/coupon/list?page=1&limit=20&sort=add_time&order=desc
    @GetMapping("/list")
    public Object list(Integer id, String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order) {
        IPage<DtsCoupon> couponIPage = couponService.list(id, name, page, limit, sort, order);

        long total = couponIPage.getTotal();
        List<DtsCoupon> records = couponIPage.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("items", records);
        return ResponseUtil.ok(map);
    }

    //http://localhost:8083/admin-ui/coupon/read?id=11
    //此优惠劵基本信息
    @RequestMapping("/read")
    public Object read(@RequestParam(value = "id") Integer id) {
        DtsCoupon coupon = couponService.findOneById(id);
        return ResponseUtil.ok(coupon);
    }

    //**此优惠劵使用的用户列表 : **
    //
    //- 接口地址 : http://
    // localhost:8083/admin/coupon/listuser?
    // page=1&limit=20&couponId=11&sort=add_time&order=desc
    @GetMapping("/listuser")
    public Object listuser(Integer couponId,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "20") Integer limit,
                           @RequestParam(defaultValue = "add_time") String sort,
                           @RequestParam(defaultValue = "desc") String order) {
        IPage<DtsCouponUser> list = couponUserService.list(couponId, page, limit, sort, order);
        long total = list.getTotal();
        List<DtsCouponUser> records = list.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("items", records);
        return ResponseUtil.ok(map);
    }

    //##### 优惠劵添加
    //
    //- 接口地址 : http://localhost:8083/admin-ui/coupon/create
    @PostMapping("/create")
    public Object insertOne(@RequestBody HashMap<String,Object> map) {
        DtsCoupon coupon = JSON.parseObject(JSON.toJSONString(map),DtsCoupon.class);
        DtsCoupon dtsCoupon = couponService.addOne(coupon);
        return ResponseUtil.ok(dtsCoupon);
    }
    //##### 优惠劵修改
    //
    //- 接口地址 : http://localhost:8083/admin-ui/coupon/update
    @RequestMapping("/update")
    public Object update(@RequestBody DtsCoupon dtsCoupon){
        DtsCoupon one = couponService.updateOne(dtsCoupon);
        return ResponseUtil.ok(one);
    }
    //##### 优惠劵删除
    //
    //- 接口地址 : http://localhost:8083/admin-ui/coupon/delete
    @RequestMapping("/delete")
    public Object delete(@RequestBody  DtsCoupon dtsCoupon){
        couponService.deleteOne(dtsCoupon);
        return ResponseUtil.ok();
    }
}
